import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet(urlPatterns = {"/login/dgut"})
public class JsonServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String appid = "javaee";
        String appsecret = "b3b52e43ccfd";
        String token = request.getParameter("token");
        String userip = request.getRemoteAddr();
        System.out.println(token);
        System.out.println(userip);

        CloseableHttpClient client1 = HttpClients.createDefault();
        HttpPost httpPost1 = new HttpPost("https://cas.dgut.edu.cn/ssoapi/v2/checkToken");

        List<NameValuePair> params1 = new ArrayList<NameValuePair>();
        params1.add(new BasicNameValuePair("appid", appid));
        params1.add(new BasicNameValuePair("appsecret", appsecret));
        params1.add(new BasicNameValuePair("token", token));
        params1.add(new BasicNameValuePair("userip", userip));
        httpPost1.setEntity(new UrlEncodedFormEntity(params1));

        HttpEntity entity1 = client1.execute(httpPost1).getEntity();

        Jsonb jsonb1 = JsonbBuilder.create();
        String postJsonString1 = new BufferedReader(new InputStreamReader(entity1.getContent()))
                .lines().collect(Collectors.joining(System.lineSeparator()));
        System.out.println(postJsonString1);
        VerifyJson verifyJson = jsonb1.fromJson(postJsonString1, VerifyJson.class);

        System.out.println(verifyJson);
        System.out.println(verifyJson.getAccess_token());
        client1.close();
        if(verifyJson.getError() == 1) response.sendRedirect("http://localhost:8080/error.html");

        String access_token = verifyJson.getAccess_token();
        String openid = verifyJson.getOpenid();

        CloseableHttpClient client2 = HttpClients.createDefault();
        HttpPost httpPost2 = new HttpPost("https://cas.dgut.edu.cn/oauth/getUserInfo");

        List<NameValuePair> params2 = new ArrayList<NameValuePair>();
        params2.add(new BasicNameValuePair("access_token", access_token));
        params2.add(new BasicNameValuePair("openid", openid));
        httpPost2.setEntity(new UrlEncodedFormEntity(params2));

        HttpEntity entity2 = client2.execute(httpPost2).getEntity();

        Jsonb jsonb2 = JsonbBuilder.create();
        String postJsonString2 = new BufferedReader(new InputStreamReader(entity2.getContent()))
                .lines().collect(Collectors.joining(System.lineSeparator()));
        Person.PersonJson personJson = jsonb2.fromJson(postJsonString2, Person.PersonJson.class);

        System.out.println(personJson);
        System.out.println(personJson.getOpenid());
        client2.close();
        if(verifyJson.getError() == 1) response.sendRedirect("http://localhost:8080/error.html");

        HttpSession session = request.getSession();
        session.setAttribute("user", personJson);
        System.out.println(session.getAttribute("user"));

        response.sendRedirect("../information.jsp");
    }
}
